package com.itxiaox.xview.demo.kotlin

import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import com.github.itxiaox.dialog.AlertDialog
import com.github.itxiaox.dialog.AlertInputDialog
import com.github.itxiaox.dialog.CommomDialog
import com.github.itxiaox.dialog.CommonDialogFragment
import com.github.itxiaox.ios.ActionSheetDialog
import com.github.itxiaox.loadingdialog.LoadingDialog
import com.itxiaox.android.xutils.ui.DensityUtils
import com.itxiaox.android.xutils.ui.UIHelper
import com.itxiaox.xview.demo.R


class DialogActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_dialog)
        findViewById<View>(R.id.btn_common).setOnClickListener { v: View? -> commonDialog() }
        findViewById<View>(R.id.btn_alert).setOnClickListener { v: View? -> alertDialog() }
        findViewById<View>(R.id.btn_input).setOnClickListener { v: View? -> inputDialog() }
        findViewById<View>(R.id.btn_dialogFragment).setOnClickListener { v: View? -> dialogFragment() }
        findViewById<View>(R.id.btn_actionsheet).setOnClickListener { v: View? -> actionSheet() }
        findViewById<View>(R.id.btn_loading).setOnClickListener { v: View? -> loadingDialog() }
    }

    private fun loadingDialog() {
        val loadingDialog = LoadingDialog(this)
        loadingDialog.setOnFinishListener { UIHelper.toast("加载完成") }
        loadingDialog.setSize(30)
                .setShowTime(5000)
                .setInterceptBack(false)
                .setLoadSpeed(LoadingDialog.Speed.SPEED_TWO)
                .show()
    }

    private fun dialogFragment() {
        val commonDialogFragment = CommonDialogFragment()
        commonDialogFragment.setOnCloseListener { dialog: DialogFragment, confirm: Boolean ->
            dialog.dismiss()
            if (confirm) {
                UIHelper.toast("点击了确定")
            } else {
                UIHelper.toast("点击了取消")
            }
        }
        //设置返回键
        commonDialogFragment.setOnKeyListener { dialog: DialogInterface?, keyCode: Int, event: KeyEvent? -> false }
        commonDialogFragment.setTitle("设置标题")
                .setContent("设置内容")
                .setNegativeButton("取消")
                .setPositiveButton("确定")
        commonDialogFragment.show(supportFragmentManager, "通用的DialogFragment")
    }

    private fun commonDialog() {
        val commomDialog = CommomDialog(this, CommomDialog.OnCloseListener { dialog: Dialog, confirm: Boolean ->
            dialog.dismiss()
            if (confirm) { //确定
                UIHelper.toast(this@DialogActivity, "点击了确定")
            } else { //取消
                UIHelper.toast(this@DialogActivity, "点击了取消")
            }
        }).setContent("我是通用对话框")
                .setNegativeButton("取消")
                .setPositiveButton("确定")
                .setTitle("标题")
        commomDialog.show()
    }

    private fun alertDialog() {
        AlertDialog(this@DialogActivity,
                DensityUtils.dip2px(250f),
                DensityUtils.dip2px(250f))
                .builder()
                .setTitle("对话框标题")
                .setMsg("对话框内容")
                .setNegativeButton("按钮一") { view: View? -> UIHelper.toast("您点击了 按钮一 ") }
                .setPositiveButton("按钮二") { view: View? -> UIHelper.toast("点击了按钮二") }
                .show()
    }

    private fun inputDialog() {
        AlertInputDialog(this@DialogActivity)
                .builder()
                .setTitle("标题")
                .setMsg("消息内容")
                .setNegativeButton("确定") { view: View? -> UIHelper.toast("您点击了确定") }
                .setPositiveButton("取消") { view: View? -> UIHelper.toast("您点击了取消") }
                .show()
    }

    private fun actionSheet() {
        val actionSheetDialog = ActionSheetDialog(this)
                .builder()
                .addSheetItem("item1",
                        ActionSheetDialog.SheetItemColor.Blue) { which: Int -> UIHelper.toast("item1 被点击了") }
                .addSheetItem("item2", ActionSheetDialog.SheetItemColor.Red) { which: Int -> UIHelper.toast("item2 被点击了") }
                .addSheetItem("item3", ActionSheetDialog.SheetItemColor.Black) { which: Int -> UIHelper.toast("item3 被点击了") }
                .setTitle("设置标题")
        actionSheetDialog.show()
    }
}